package com.sduept.nwld.dataview.controller.core;

import com.alibaba.fastjson.JSONObject;
import com.sduept.bigdata.web.jsf.controller.AbstractController;
import com.sduept.cim.model.protection.ProtectionEquipment;
import com.sduept.cim.model.wires.Breaker;
import com.sduept.nwld.dataserver.manager.cim.CimManager;
import com.sduept.nwld.dataserver.util.LibTreeNode;

import org.primefaces.event.NodeExpandEvent;
import org.primefaces.event.NodeSelectEvent;
import org.primefaces.model.DefaultTreeNode;
import org.primefaces.model.TreeNode;
import org.springframework.beans.factory.annotation.Autowired;

import javax.annotation.PostConstruct;
import javax.inject.Named;
import javax.faces.view.ViewScoped;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Named
@ViewScoped
public class EquipmentSingleSelectorFaultReportLayerController extends AbstractController {

	private static final long serialVersionUID = 3655966016971835804L;

	@Autowired
	private CimManager cimMan;

	public static final String TREE_TYPE_Dir = "1";
	public static final String TREE_TYPE_All = "All";
	String subName;
	private TreeNode root;
	private TreeNode selectedNode;
	List<ProtectionEquipment> protects = new ArrayList<ProtectionEquipment>();
	List<ProtectionEquipment> AllProtects = new ArrayList<ProtectionEquipment>();
	List<Breaker> breakers = new ArrayList<Breaker>();
	List<Breaker> AllBreakers = new ArrayList<Breaker>();
	LibTreeNode allPtotectionsNode;
	LibTreeNode allBreakersNode;
	TreeNode snode;
	String subStationId = "";
	String protectionType = "";
	String deciveId = "";
	private String selectedEquipmentJson = "{}";

	@PostConstruct
	public void init() {
		subStationId = getParameter("stationId");
		protectionType = getParameter("protectionType");
		deciveId = getParameter("deviceId");
		subName = cimMan.getStationById(subStationId).getName();
		initTree();
	}

	private void initTree() {
		root = new LibTreeNode();
		snode = new DefaultTreeNode(TREE_TYPE_Dir, subName, root);
		if ("zbhdz".equals(protectionType)) { // 主保护
			protects = cimMan.listMainEquipmentsByStationIdAndprotectedId(subStationId, deciveId);
			for (int i = 0; i < protects.size(); i++) {
				ProtectionEquipment protect = protects.get(i);
				new DefaultTreeNode(ProtectionEquipment.class.getSimpleName(), protect, snode);
			}
			allPtotectionsNode = new LibTreeNode(TREE_TYPE_All, "保护设备", snode);
		} else if ("dlqbhdzxx".equals(protectionType)) {// 断路器保护
			protects = cimMan.getBreakerProtectByPrimaryIdAndStationId(deciveId, subStationId);
			for (int i = 0; i < protects.size(); i++) {
				ProtectionEquipment protect = protects.get(i);
				new DefaultTreeNode(ProtectionEquipment.class.getSimpleName(), protect, snode);
			}
			allPtotectionsNode = new LibTreeNode(TREE_TYPE_All, "保护设备", snode);
		} else if ("dlqdzxx".equals(protectionType)) {// 断路器
			breakers = cimMan.getBreakersById(deciveId, subStationId);
			for (int i = 0; i < breakers.size(); i++) {
				Breaker breaker = breakers.get(i);
				new DefaultTreeNode(Breaker.class.getSimpleName(), breaker, snode);
			}
			allBreakersNode = new LibTreeNode(TREE_TYPE_All, "断路器设备", snode);
		}
		snode.setExpanded(true);
	}

	public void selectEquipment() {
		// 单选
		String className = "";
		String data = "";
		if(selectedNode != null && !TREE_TYPE_All.equals(selectedNode.getType())
				&& !TREE_TYPE_Dir.equals(selectedNode.getType())) {
			Object o = selectedNode.getData();
			if(o instanceof ProtectionEquipment) {
				ProtectionEquipment p = (ProtectionEquipment) o;
				className = ProtectionEquipment.class.getSimpleName();
				data = JSONObject.toJSONString(p);
			}else if(o instanceof Breaker) {
				Breaker p = (Breaker) o;
				className = Breaker.class.getSimpleName();
				data = JSONObject.toJSONString(p);
			}
		}
		JSONObject r = new JSONObject();
		r.put("className", className);
		r.put("data", data);
		r.put("type", protectionType);
		selectedEquipmentJson = r.toJSONString();
	}

	public void onNodeSelect(NodeSelectEvent event) {
		selectedNode = event.getTreeNode();
	}

	public void onNodeExpand(NodeExpandEvent event) {
		selectedNode = event.getTreeNode();
		if ("All".equals(selectedNode.getType())) {
			if ("保护设备".equals(selectedNode.getData())) {
				AllProtects = cimMan.getProtectionEquipmentsByStationId(subStationId);
				for (int i = 0; i < AllProtects.size(); i++) {
					ProtectionEquipment protect = AllProtects.get(i);
					new DefaultTreeNode(ProtectionEquipment.class.getSimpleName(), protect,
							allPtotectionsNode);
				}
			} else if ("断路器设备".equals(selectedNode.getData())) {
				AllBreakers = cimMan.getBreakersByStationId(subStationId);
				for (int i = 0; i < AllBreakers.size(); i++) {
					Breaker breaker = AllBreakers.get(i);
					new DefaultTreeNode(Breaker.class.getSimpleName(), breaker, allBreakersNode);
				}
			}
		}
	}
	
	public void openProtectAccountDialog(){
		 Map<String,Object> options = new HashMap<String, Object>();
	        options.put("resizable", true);
	        options.put("draggable", true);
	        options.put("modal", true);
	        options.put("contentWidth", 1200);
	        options.put("contentheight", 700);
	        options.put("height", 700);
	        options.put("width", 1250);
	        openDialog("/ui/dataview/devicefault/protectAccountDialog", null, options);
	}

	public TreeNode getRoot() {
		return root;
	}

	public void setRoot(TreeNode root) {
		this.root = root;
	}

	public TreeNode getSelectedNode() {
		return selectedNode;
	}

	public void setSelectedNode(TreeNode selectedNode) {
		this.selectedNode = selectedNode;
	}

	public String getSelectedEquipmentJson() {
		return selectedEquipmentJson;
	}

	public void setSelectedEquipmentJson(String selectedEquipmentJson) {
		this.selectedEquipmentJson = selectedEquipmentJson;
	}

}
